package com.ca.ridersclubapplication.view.card;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.view.bar.MiniScoreProcessBar;

import java.util.ArrayList;
import java.util.List;

public class PostCard extends ConstraintLayout {

    private Long grade = 0L;

    private final List<ImageView> list = new ArrayList<>();

    public MiniScoreProcessBar scoreBar;
    public Button postButton;
    public TextView gradeView;

    public PostCard(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public PostCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PostCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.post_score_card, this);
        scoreBar = view.findViewById(R.id.bar_score);
        postButton = view.findViewById(R.id.btn_post);

        list.add(scoreBar.iv1);
        list.add(scoreBar.iv2);
        list.add(scoreBar.iv3);
        list.add(scoreBar.iv4);
        list.add(scoreBar.iv5);

        gradeView = view.findViewById(R.id.tv_grade);
        gradeView.setText("");

        //  点击事件
        for (int i = 0; i < list.size(); i++) {
            ImageView iv = list.get(i);

            ViewGroup.LayoutParams params = iv.getLayoutParams();
            params.height = (int) (params.height * 1.5);
            params.width = (int) (params.width * 1.5);

            final int position = i;
            iv.setOnClickListener(v -> {
                grade = (long) (position + 1);
                scoreBar.setScore(grade);
                gradeView.setText(String.valueOf(grade));
            });
        }
    }

    public Long getGrade() {
        return grade;
    }
}
